package com.all.proxy.controller;

import com.all.proxy.handler.ByteBuddyProxyHandler;
import com.all.proxy.handler.CglibProxyHandler;
import com.all.proxy.handler.JavassistProxyHandler;
import com.all.proxy.handler.JdkProxyHandler;
import com.all.proxy.service.IUserService;
import com.all.proxy.service.impl.FriendServiceImpl;
import com.all.proxy.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author LY
 * @create 2024/3/7 14:07
 */
@RestController
@Slf4j
public class ProxyController {

    @GetMapping("/getUser/{uId}")
    public String getUser(@PathVariable Long uId){
        log.info("获取用户信息|getUser|{}", uId);
        IUserService userService = new UserServiceImpl();
        JdkProxyHandler handler = new JdkProxyHandler(userService);
        IUserService user = (IUserService) handler.getProxy(userService.getClass());
        String userInfo = user.getUser(uId);
        return userInfo;
    }

    @GetMapping("/getUserInfo/{uId}")
    public String getUserInfo(@PathVariable Long uId){
        log.info("获取用户信息接口实现代理|getUserInfo|{}", uId);
        IUserService userService = new UserServiceImpl();
        CglibProxyHandler handler = new CglibProxyHandler(userService);
        IUserService user = (IUserService) handler.getProxyInterface(userService.getClass());
        String userInfo = user.getUser(uId);
        return userInfo;
    }

    @GetMapping("/getFriend/{fId}")
    public String getFriend(@PathVariable Long fId){
        log.info("获取用户好友信息非接口实现代理|getFriend|{}", fId);
        CglibProxyHandler handler = new CglibProxyHandler();
        FriendServiceImpl friend = (FriendServiceImpl) handler.getProxy(FriendServiceImpl.class);
        String friendInfo = friend.getFriend(fId);
        return friendInfo;
    }

    @GetMapping("/getUser2/{uId}")
    public String getUser2(@PathVariable Long uId){
        log.info("获取用户信息非接口实现代理|getUser2|{}", uId);
        JavassistProxyHandler handler = new JavassistProxyHandler();
        UserServiceImpl user = (UserServiceImpl) handler.getProxy(UserServiceImpl.class);
        String userInfo = user.getUser(uId);
        return userInfo;
    }

    @GetMapping("/getUserInfo2/{uId}")
    public String getUserInfo2(@PathVariable Long uId){
        log.info("获取用户信息接口实现代理|getUserInfo2|{}", uId);
        IUserService userService = new UserServiceImpl();
        JavassistProxyHandler handler = new JavassistProxyHandler(userService);
        IUserService user = (IUserService) handler.getProxyInterface(userService.getClass());
        String userInfo = user.getUser(uId);
        return userInfo;
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        // 非接口实现代理
//        JavassistProxyHandler handler = new JavassistProxyHandler();
//        UserServiceImpl user = (UserServiceImpl) handler.getProxy(UserServiceImpl.class);
//        String userInfo = user.getUser(123456L);
//        System.out.println(userInfo);

        // 接口实现代理
//        IUserService userService = new UserServiceImpl();
//        JavassistProxyHandler handler = new JavassistProxyHandler(userService);
//        IUserService user = handler.getProxyInterface(userService.getClass());
//        String userInfo = user.getUser(123456L);
//        System.out.println(userInfo);

        IUserService userService = new UserServiceImpl();
        ByteBuddyProxyHandler handler = new ByteBuddyProxyHandler(userService);
        IUserService user = handler.getProxy(IUserService.class);
        String userInfo = user.getUser(123456L);
        System.out.println(userInfo);
    }
}
